<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use GatewayWorker\Lib\Gateway;
use think\Exception;
class Group extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function join()
    {
        try{
            $params=$this->request->param();

            $uid=$params['app_id'].'_'.$params['uid'];

            $to_group_id=$params['app_id'].'_'.$params['group_id'];

            $gateway=new Gateway();
            $client_ids=$gateway::getClientIdByUid($uid);

            foreach ($client_ids as $index => $client_id) {
                $gateway::joinGroup($client_id,$to_group_id);
            }

            $this->apiJson(0,[
                'group_id'=>$params['group_id'],
            ],'已经踢出群组');

        }catch (Exception $exception){
            $this->apiJson(1,'',$exception->getMessage());
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function leave()
    {
        try{
            $params=$this->request->param();

            $uid=$params['app_id'].'_'.$params['uid'];

            $to_group_id=$params['app_id'].'_'.$params['group_id'];

            $gateway=new Gateway();
            $client_ids=$gateway::getClientIdByUid($uid);

            foreach ($client_ids as $index => $client_id) {
                $gateway::leaveGroup($client_id,$to_group_id);
            }
            $this->apiJson(0,[
                'group_id'=>$params['group_id'],
            ],'已经踢出群组');

        }catch (Exception $exception){
            $this->apiJson(1,'',$exception->getMessage());
        }
    }

    public function ungroup()
    {
        try{
            $params=$this->request->param();

            $to_group_id=$params['app_id'].'_'.$params['group_id'];

            Gateway::ungroup($to_group_id);
            $this->apiJson(0,[
                'group_id'=>$params['group_id'],
            ],'解散成功');

        }catch (Exception $exception){
            $this->apiJson(1,'',$exception->getMessage());
        }
    }

    public function count(){
        try{
            $params=$this->request->param();

            $to_group_id=$params['app_id'].'_'.$params['group_id'];

            $num=Gateway::getUidCountByGroup($to_group_id);
            $this->apiJson(0,[
                'group_id'=>$params['group_id'],
                'client_num'=>$num
            ],'发送成功');

        }catch (Exception $exception){
            $this->apiJson(1,'',$exception->getMessage());
        }
    }

    public function user(){
        try{
            $params=$this->request->param();

            $to_group_id=$params['app_id'].'_'.$params['group_id'];

            $user=Gateway::getUidListByGroup($to_group_id);
            $this->apiJson(0,[
                'group_id'=>$params['group_id'],
                'user'=>$user
            ],'发送成功');

        }catch (Exception $exception){
            $this->apiJson(1,'',$exception->getMessage());
        }
    }
}
